$(function(){
	$("#content").click(function(){
		$("#removeAdd").click();
	})
})
//添加留言界面
$(function(){
	$("#add").click(function(){
		$("#suggadd").show(500);
		$("#suggCont").val("");
		$("#suggCont").focus();
	});
	$("#removeAdd").click(function(){
		$("#suggadd").hide(500);
		$("#suggCont").val("");
		$("#ques").html("不知兄台有何高见");
	});
});
//添加建言
$(function(){
	$("#suggGo").click(function(){
		var contenta=$("#suggCont").val();
		if(contenta.length==0){
			$("#ques").html("请输入建言");
		}else{
			var proNum=Math.floor(Math.random()*5);
			$.ajax({
				type:"POST",
				url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/student/suggadd.php",
				dataType:"json",
				data:{
					content:contenta,
					propic:proNum
				},
				success:function(x,y,z){
					if(x.key=="success"){
						$("#ques").html("添加成功！");
						$("#suggCont").val("");
						setTimeout(function(){
							window.location.reload(false);
						},500);
					}else{
						$("#ques").html("添加失败！");
					}
				},
				error:function(jqXHR){
				    $("#ques").html("发生错误"+jqXHR.status);
				}
			});
		}
	});
});
//查询所有建言
$(function(){
	$.ajax({
		type:"GET",
		url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/selectAll.php",
		dataType:"json",
		success:function(x,y,z){
			var content=$("#content");
			var overHead=$("#overHead");
			console.log(x)
			if(x.sugg.length==1){
				content.append("<h3 id='noSugg'>暂无建言</h3>");
			}else{
				content.append("<div class='container'></div>");
				overHead.append("<div class='container'></div>");
				for(var n=1;n<x.sugg.length;n++){
					content.children(".container").append(
						"<div class='row theCont' id='"+x.sugg[n].sugg_id+"'>"+
							"<div class='col-xs-12'>"+
								"<div class='proAcon row'>"+
									"<div class='propic col-xs-2'>"+"<img src='image/"+x.sugg[n].sugg_propic+".png'>"+"</div>"+
									"<div class='cont col-xs-10'>"+x.sugg[n].sugg_content+"</div>"+
								"</div>"+
								"<div>"+
									"<div class='sugg_date'>"+x.sugg[n].sugg_date+"</div>"+
								"</div>"+
							"</div>"+
						"</div>"
					)
				};
				for(var m=1;m<x.resp.length;m++){
					var target=$("#content .container").find("#"+x.resp[m].resp_suggid);
					target.append(
						"<div class='col-xs-2'></div>"+
						"<div class='col-xs-10'>"+
							"<div class='row'>"+
								"<div class='propica col-xs-2'>"+"<img src='image/bwc.jpg'>"+"</div>"+
								"<div class='conta col-xs-10'>"+x.resp[m].resp_content+"</div>"+
							"</div>"+
							"<div>"+
								"<div class='sugg_date'>"+x.resp[m].resp_date+"</div>"+
							"</div>"+
						"</div>"
					);
					target.appendTo($("#overHead .container"));
				}
			}
		},
		error:function(jqXHR){
		    console.log("发生错误"+jqXHR.status);
		}
	});
});